//
//  HZBaseTc.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/16.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit

class HZBaseTc: UITableViewController, HZInitViewDelegate {

  

    //懒加载 view
    lazy var initView:HZInitView = HZInitView()
    
    //定义一个 BOOL 值来判断是否用户已经登录了
    var login:Bool = HZOAuthVM.sharedVM.loginEnd
    
    override func loadView() {
        
        
        if login==true {
            super.loadView()
            
        }else{
            
            self.view = initView
        }
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        

        
        //设置代理
        initView.delegate = self
        
        //接收登录按钮的通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "loginP", name: "login", object: nil)
        
        //添加导航栏的按钮
        setupUI()
        
        
        if login==true {
        
            navigationItem.leftBarButtonItem = nil
            navigationItem.rightBarButtonItem = nil
        }
        
    }

    // MARK: -  实现添加导航栏的按钮
    private func setupUI(){
        
        let leftItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "register")
        
        navigationItem.leftBarButtonItem = leftItem
        
        let rightItem = UIBarButtonItem(title: "登录", style: .Plain, target: self, action: "loginP")
        
        rightItem.tintColor = UIColor.grayColor()
        
        navigationItem.rightBarButtonItem = rightItem
        
    }
    
    // MARK: -  实现注册代理方法
    func register() {
        
        print("注册了")
    }
    
    
    // MARK: -  实现登录按钮的通知
    func loginP(){
        
        print("登录了")
        
        
        let oauthVc = HZOAuthView()
        
        presentViewController(UINavigationController(rootViewController: oauthVc), animated: true, completion: nil)
        
        
    }
    
    
    // 对象销毁时调用(析构函数)
    deinit{
        
        //销毁时移除通知中心
       NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }
    
    
    

}
